/*
 * @Author: 0x9DEFA478
 * @Date: 2022-07-24 17:33:59
 * @LastEditTime: 2022-08-20 20:33:06
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper65_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper65_PRG_Read(Huint16 Address,void* v);
extern Huint16 H_NES_Mapper65_PRG_Read16(Huint16 Address,void* v);
extern void H_NES_Mapper65_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
extern void H_NES_Mapper65_CHR_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper65_CHR_Read(Huint16 Address,void* v);
extern void H_NES_Mapper65_CPUTick(int CPUCycle,void* v);




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper65




void H_NES_Mapper65_MapperUpdate(void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint32 ROM_Size;
  Huint32 Offset;
  Hbyte* t_bytep;

  Console=v;


  p=Console->Cartridge.Mapper.PRG_ROM;
  ROM_Size=Console->Cartridge.Mapper.PRG_ROM_Size;
  if((Console->ThisMapper.Reg_9000&0x80U)==0x00U){

    Offset=((Huint32)Console->ThisMapper.Reg_8000)<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[0]=t_bytep;

    Offset=((Huint32)Console->ThisMapper.Reg_A000)<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[1]=t_bytep;

    Offset=ROM_Size-0x2000*2;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[2]=t_bytep;

  }else{

    Offset=ROM_Size-0x2000*2;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[0]=t_bytep;

    Offset=((Huint32)Console->ThisMapper.Reg_A000)<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[1]=t_bytep;

    Offset=((Huint32)Console->ThisMapper.Reg_8000)<<13;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.PRG_Banks.Base8000_8KB[2]=t_bytep;

  }

  p=Console->Cartridge.Mapper.CHR_ROM;
  ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
  if(ROM_Size==0U){
    ROM_Size=0x2000U;
  }
  for(int i=0;i<8;i++){
    Offset=((Huint32)Console->ThisMapper.Reg_B000_B007[i])<<10;
    Offset=Offset%ROM_Size;
    t_bytep=&p[Offset];
    Console->ThisMapper.CHR_Banks.Base0000_1KB[i]=t_bytep;
  }
  
}

void H_NES_Mapper65_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console){

  if((Address>=0xB000U)&&(Address<0xB008U)){
    Console->ThisMapper.Reg_B000_B007[Address-0xB000U]=Value;
    H_NES_Mapper65_MapperUpdate(Console);
    return;
  }
  
  switch(Address){
    case 0x8000U:
      Console->ThisMapper.Reg_8000=Value;
      H_NES_Mapper65_MapperUpdate(Console);
      break;
    case 0x9000U:
      Console->ThisMapper.Reg_9000=Value;
      H_NES_Mapper65_MapperUpdate(Console);
      break;
    case 0x9001U:
      
      if(Console->PPU.VRAM_ScreenType!=0x03U){
        switch(Value>>6){
          case 0x00U:
            //命名表水平排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x02U;
            break;
          case 0x01U:
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.VRAM_ScreenType=0x00U;
            break;
          case 0x02U:
            //命名表垂直排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x01U;
            break;
          case 0x03U:
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=0x00U;
            break;
          default:
            break;
        }
      }

      break;
    case 0x9003U:
      Console->ThisMapper.Reg_9003=Value;
      Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ);
      break;
    case 0x9004U:
      Console->ThisMapper.Count=Console->ThisMapper.CountReload;
      Console->CPU.Status&=(~vH_NES_CPU_Status_IRQ_ExtIRQ);
      break;
    case 0x9005U:
      Console->ThisMapper.CountReload=(Console->ThisMapper.CountReload&0x00FFU)|(((Huint16)Value)<<8);
      break;
    case 0x9006U:
      Console->ThisMapper.CountReload=(Console->ThisMapper.CountReload&0xFF00U)|(Huint16)Value;
      break;
    case 0xA000U:
      Console->ThisMapper.Reg_A000=Value;
      H_NES_Mapper65_MapperUpdate(Console);
      break;
    default:
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      break;
  }
}




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_8000;
  p[1]=Console->ThisMapper.Reg_9000;
  p[2]=Console->ThisMapper.Reg_9003;
  p[3]=Console->ThisMapper.Reg_A000;

  p[4]=(Hbyte)Console->ThisMapper.Count;
  p[5]=(Hbyte)(Console->ThisMapper.Count>>8);
  p[6]=(Hbyte)Console->ThisMapper.CountReload;
  p[7]=(Hbyte)(Console->ThisMapper.CountReload>>8);
  
  for(i=0;i<8;i++){
    p[8+i]=Console->ThisMapper.Reg_B000_B007[i];
  }

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  int i;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.Reg_8000=p[0];
  Console->ThisMapper.Reg_9000=p[1];
  Console->ThisMapper.Reg_9003=p[2];
  Console->ThisMapper.Reg_A000=p[3];

  Console->ThisMapper.Count=((Huint16)p[4])|(((Huint16)p[5])<<8);
  Console->ThisMapper.CountReload=((Huint16)p[6])|(((Huint16)p[7])<<8);
  
  for(i=0;i<8;i++){
    Console->ThisMapper.Reg_B000_B007[i]=p[8+i];
  }
  
  H_NES_Mapper65_MapperUpdate(Console);
}

static void Reset(void* v){
  H_NES_Console* Console;
  int i;

  Console=v;

  Console->ThisMapper.Reg_8000=0x00U;
  Console->ThisMapper.Reg_9000=0x00U;
  Console->ThisMapper.Reg_9003=0x00U;
  Console->ThisMapper.Reg_A000=0x01U;

  Console->ThisMapper.Count=0U;
  Console->ThisMapper.CountReload=0U;
  
  for(i=0;i<8;i++){
    Console->ThisMapper.Reg_B000_B007[i]=0x00U;
  }
  
  H_NES_Mapper65_MapperUpdate(Console);
}

int H_NES_Mapper65_Init(void* v){
  H_NES_Console* Console;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<0){
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Reset(Console);

  Console->ThisMapper.PRG_Banks.Base8000_8KB[3]=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x2000U];


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper65_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper65_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper65_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper65_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper65_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper65_CHR_Read;

  Console->Cartridge.Progress.Size=16+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;

  Console->Cartridge.Option.CPUTick=H_NES_Mapper65_CPUTick;
  
  


  return vH_NES_Result_OK;
}




#undef ThisMapper



